import { RootThunkAction } from "@/types/store"
import { http } from "@/utils/http"
import type {
  UserResponse,
  UserProfileResponse,
  UserProfile,
} from "@/types/data"

export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = (await http.get("/user")) as UserResponse 
    dispatch({ type: "user/getuser", payload: res.data }) 
  }
}

export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = (await http.get("/user/profile")) as UserProfileResponse
    dispatch({ type: "user/getprofile", payload: res.data })
  }
}

export const updateUserProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch("/user/profile", userProfile)
    dispatch({ type: "user/update", payload: userProfile })
  }
}
